/*
** init.c for  in /home/gomes_m//rattrapage/server
** 
** Made by mickael gomes
** Login   <gomes_m@epitech.net>
** 
** Started on  Tue Jul 23 12:18:04 2013 mickael gomes
** Last update Wed Jul 31 01:16:15 2013 mickael gomes
*/

#include		<unistd.h>
#include		<stdio.h>
#include		<sys/types.h>
#include		<sys/socket.h>
#include		<fcntl.h>
#include		<stdlib.h>
#include		"struct.h"
#include		"prototypes.h"

void			init_socket(t_socket *socket, int port)
{
  struct protoent	*pe;

  pe = xgetprotobyname("TCP");
  socket->sock = xsocket(AF_INET, SOCK_STREAM, pe->p_proto);
  socket->sin.sin_family = AF_INET;
  socket->sin.sin_port = htons(port);
  socket->sin.sin_addr.s_addr = INADDR_ANY;
  xbind(socket->sock, (const struct sockaddr*)&socket->sin,
	sizeof(socket->sin));
  socket->max_client = xgetrlimit();
  xlisten(socket->sock, socket->max_client - 3);
  xfcntl(socket->sock, F_GETFL, 0);
  xfcntl(socket->sock, F_SETFL, 0 | O_NONBLOCK);
  socket->client_len = sizeof(socket->sin_client);
  socket->cast_sin_client = (struct sockaddr *)&socket->sin_client;
  socket->cast_client_len = (socklen_t *)&socket->client_len;
  socket->fds = 4;
}

void			init_list(t_list *list, char **env)
{
  t_cmd			*cmd;

  list->cmd = xmalloc(sizeof(*cmd));
  list->current_dir = xmalloc(1024 * sizeof(char));
  list->cmd->pos = -1;
  list->cmd->arguments = NULL;
  list->cmd->cmd = NULL;
  list->cmd->result = xmalloc(1024 * sizeof(char));
  list->fd = 0;
  list->current_dir = my_strcat_plus(getcwd(list->current_dir, 1024),
				     "repo", '/');
  list->cmd->home = list->current_dir;
  list->disconnected = 0;
  list->cmd->env = env;
  list->cmd->paths = get_env_paths(env);
  list->local = 0;
  list->next = NULL;
}

void			init_tmp(t_list *tmp)
{
  t_cmd			*cmd;

  tmp->cmd->arguments = xmalloc(1024 * sizeof(char));
  tmp->cmd = xmalloc(sizeof(*cmd));
  tmp->cmd->arguments = my_strcat_plus(getcwd(tmp->cmd->arguments, 1024),
				       "repo", '/');
  tmp->cmd->home = tmp->cmd->arguments;
  tmp->fd = 0;
}
